#!/usr/bin/env python

import sys, os
from newsletter_class import Newsletter

def get_templates ( liwe ):
	n = Newsletter ( liwe )
	return { "templates" : n.get_templates () }

#{{{def _create_dataset ( start_point, lines, rows, tot_rows ):
def _create_dataset ( start_point, lines, rows, tot_rows ):
    res = {}

    count = 0
    for v in rows:
        v [ "_pos" ] = start_point + count
        res [ "row%d" % count ] = v
        count += 1

    res [ 'rows' ] = tot_rows
    res [ 'from_row' ] = start_point
    res [ 'to_row' ] = start_point + count
    res [ 'lines' ] = lines

    return res
#}}}
#{{{def get_subscriber ( liwe, id_subscriber = 0 ):
def get_subscriber ( liwe, id_subscriber = 0, email = '', uid = 0 ):
	"""
	:INPUT:
		- id_subscriber:	SAFE_NUMBER
		- uid:				SAFE_NUMBER
		- email:			SAFE_DB_STRING
	"""
	n = Newsletter( liwe )
	return { "subscriber" : n.get_subscriber ( id_subscriber = id_subscriber, email = email, uid = uid ) }
#}}}
#{{{def list_subscriber ( liwe, email = '', lastname = '', name = '', localita = '', provincia = '', nazione = '', azienda = '', area = '', bloccato = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
def list_subscriber ( liwe, email = '', lastname = '', name = '', localita = '', provincia = '', nazione = '', azienda = '', area = '', bloccato = '', gruppo = 0, quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):

    """
    :INPUT:
        - email:    SAFE_DB_STRING
        - lastname:  SAFE_DB_STRING
        - name: SAFE_DB_STRING
        - localita: SAFE_DB_STRING
        - provincia:    SAFE_DB_STRING
        - nazione:  SAFE_DB_STRING
        - azienda:  SAFE_DB_STRING
        - area: SAFE_DB_STRING
        - bloccato: SAFE_NUMBER
        - gruppo: SAFE_NUMBER
        - quant:        SAFE_NUMBER
        - _X_LINES:     SAFE_NUMBER
        - _X_START_POINT:   SAFE_NUMBER
        - _X_PAGE:      SAFE_NUMBER
    """
    use_dataset = 0

    if _X_LINES != -1:
        quant = _X_LINES
        use_dataset = 1

    if _X_START_POINT:
        start_point = _X_START_POINT
    else:
        start_point = _X_PAGE * quant

    n = Newsletter ( liwe )
    resp = n.list_subscriber ( email = email, lastname = lastname, name = name, localita = localita, provincia = provincia, nazione = nazione, azienda = azienda, area = area, bloccato = bloccato, gruppo = gruppo, quant = quant, start_point = start_point, do_count = use_dataset )


    rows = resp [ 'rows' ]
    tot_rows =  resp [ 'tot_rows' ]

    if not use_dataset:
        return { 'subscriber': rows }

    return _create_dataset ( start_point, quant, rows, tot_rows )
#}}}
#{{{def set_subscriber ( liwe, id_subscriber = 0, uid = 0, email = '', lastname = '', name = '', localita = '', provincia = '', nazione = '', azienda = '', area = '', bloccato = 0, note = '' ):
def set_subscriber ( liwe, id_subscriber = 0, uid = 0, email = '', lastname = '', name = '', localita = '', provincia = '', nazione = '', azienda = '', area = '', bloccato = 0, note = '', gruppo = 0 ):
	"""
	:INPUT:
		- id_subscriber:    SAFE_NUMBER
		- uid:  SAFE_NUMBER
		- email:    SAFE_DB_STRING
		- lastname:  SAFE_DB_STRING
		- name: SAFE_DB_STRING
		- localita: SAFE_DB_STRING
		- provincia:    SAFE_DB_STRING
		- nazione:  SAFE_DB_STRING
		- azienda:  SAFE_DB_STRING
		- area: SAFE_DB_STRING
		- bloccato: SAFE_NUMBER
		- gruppo: SAFE_NUMBER
		- note: SAFE_DB_STRING
	"""
	n = Newsletter ( liwe )
	resp = n.set_subscriber ( id_subscriber = id_subscriber, uid = uid, email = email, lastname = lastname, name = name, localita = localita, provincia = provincia, nazione = nazione, azienda = azienda, area = area, bloccato = bloccato, note = note, gruppo = gruppo )
	return { 'subscriber' : resp }

#}}}
#{{{def get_group ( liwe, id_group = 0 ):
def get_group ( liwe, id_group = 0 ):
	"""
	:INPUT:
		- id_group :	SAFE_NUMBER
	"""
	n = Newsletter ( liwe )
	return { 'group' : n.get_group ( id_group = id_group ) }
#}}}

def list_group ( liwe, quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):

    """
    :INPUT:
        - quant:        SAFE_NUMBER
        - _X_LINES:     SAFE_NUMBER
        - _X_START_POINT:   SAFE_NUMBER
        - _X_PAGE:      SAFE_NUMBER
    """
    use_dataset = 0

    if _X_LINES != -1:
        quant = _X_LINES
        use_dataset = 1

    if _X_START_POINT:
        start_point = _X_START_POINT
    else:
        start_point = _X_PAGE * quant

    n = Newsletter ( liwe )
    resp = n.list_group ( quant = quant, start_point = start_point, do_count = use_dataset )


    rows = resp [ 'rows' ]
    tot_rows =  resp [ 'tot_rows' ]

    if not use_dataset:
        return { 'group': rows }

    return _create_dataset ( start_point, quant, rows, tot_rows )
#{{{def mail_subscribe ( liwe, id_subscriber = 0, uid = 0, email = '', lastname = '', name = '', localita = '', provincia = '', nazione = '', azienda = '', area = '', bloccato = 0, note = '' ):
def mail_subscribe ( liwe, id_subscriber = 0, uid = 0, email = '', lastname = '', name = '', localita = '', provincia = '', nazione = '', azienda = '', area = '' ):
	"""
	:INPUT:
		- id_subscriber:    SAFE_NUMBER
		- uid:  SAFE_NUMBER
		- email:    SAFE_DB_STRING
		- lastname:  SAFE_DB_STRING
		- name: SAFE_DB_STRING
		- localita: SAFE_DB_STRING
		- provincia:    SAFE_DB_STRING
		- nazione:  SAFE_DB_STRING
		- azienda:  SAFE_DB_STRING
		- area: SAFE_DB_STRING
    """
	n = Newsletter ( liwe )
	resp = n.mail_subscribe ( id_subscriber = id_subscriber, uid = uid, email = email, lastname = lastname, name = name, localita = localita, provincia = provincia, nazione = nazione, azienda = azienda, area = area )
	return { 'mail' : resp }

#}}}
def mail_unsubscribe ( liwe, id_subscriber = 0 ):
	"""
	:INPUT:
		- id_subscriber:    SAFE_NUMBER
    """
	n = Newsletter ( liwe )
	resp = n.mail_unsubscribe ( id_subscriber = id_subscriber )
	return { 'mail' : resp }
	
def subscribe_confirm ( liwe, code = '' ):
	"""
	:INPUT:
			- code = SAFE_DB_STRING
	"""
	n = Newsletter ( liwe )
	resp = n.subscribe_confirm ( code = code )
	#liwe.layman.redirect   (  "%s?/page/nlconfirm" % liwe.base_url )

def unsubscribe_confirm ( liwe, id_subscriber = 0 ):
	
	"""
	:INPUT:
		- id_subscriber:    SAFE_NUMBER
	"""
	n = Newsletter ( liwe )
	resp = n.unsubscribe_confirm ( id_subscriber )
	#liwe.layman.redirect   (  "%s?/page/unsubconfirm" % liwe.base_url )


#{{{set_subscriber_rel ( liwe, id_subscriber = 0, rels = '' ):
def set_subscriber_rel ( liwe, id_subscriber = 0, rels = '' ):
	"""
	:INPUT:
		- id_subscriber:	SAFE_NUMBER
		- rels:				SAFE_DB_STRING
	"""
	n = Newsletter ( liwe )
	resp = n.set_subscriber_rel ( id_subscriber = id_subscriber, rels =  rels )
	return { 'rels' : resp }
#}}}
#{{{def get_info ( liwe, id_table.name = 0 ):
def get_info ( liwe, id_info = 0 ):
	"""
	:INPUT:
				- id_info:            SAFE_NUMBER
	"""
	n = Newsletter ( liwe )
	return { "info" : n.get_info ( id_info = id_info ) }
#}}}
#{{{def list_info ( liwe, title = '', sender = '', subject = '', localita = '', bloccato = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
def list_info ( liwe, title = '', sender = '', subject = '', quant = 0, free= -1, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):

	"""
	:INPUT:
		- title:    SAFE_DB_STRING
		- sender:   SAFE_DB_STRING
		- subject:  SAFE_DB_STRING
		- quant:        SAFE_NUMBER
		- free:        SAFE_NUMBER
		- _X_LINES:     SAFE_NUMBER
		- _X_START_POINT:   SAFE_NUMBER
		- _X_PAGE:      SAFE_NUMBER
	"""
	use_dataset = 0

	if _X_LINES != -1:
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	n = Newsletter ( liwe )
	resp = n.list_info ( title = title, sender = sender, subject = subject, free = free, quant = quant, start_point = start_point, do_count = use_dataset )


	rows = resp [ 'rows' ]
	tot_rows =  resp [ 'tot_rows' ]

	if not use_dataset:
		return { 'info': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )
#}}}
#{{{check_rel ( liwe, id_subscriber = 0 ):
def check_rel ( liwe, id_subscriber = 0 , subscriber_only = 0 , id_info = 0):
	"""
	:INPUT:
		- id_subscruber: SAFE_NUMBER
		- subscriber_only : SAFE_NUMBER
		- id_info	: SAFE_NUMBER
	"""
	n = Newsletter ( liwe )
	return { 'check' : n.check_rel ( id_subscriber =  id_subscriber, subscriber_only = subscriber_only, id_info = id_info ) }
#}}}








